package org.hot100_2;

public class leetcode34 {
    public int[] searchRange(int[] nums, int target) {
        int [] res= new int[2];
        int left = findIndex(nums, target);
        int right = findIndex(nums, target + 1) - 1;
        if (left== nums.length || nums[left]!=target){
            res[0]=-1;
            res[1]=-1;
        }else {
            res[0]=left;
            res[1]=right;
        }

        return res;
    }

    private int findIndex(int [] nums,int target){
        int left= 0;
        int right = nums.length-1;
        while (left<=right){
            int mid =(left+right)>>>1;
            if (target<=nums[mid]){
                //当target==nums[mid]时，执行的是：right=mid-1; 是为了继续在左半部分查找
                right=mid-1;
            }else {

                left=mid+1;
            }
        }
        return left;
    }
}
